<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  function unique(arr) {
    const res = []
    arr.forEach(item => {

      if (res.indexOf(item) < 0) {
        res.push(item)
      }

    })
    return res
  }

  //使用set方法
  function unique(arr) {
    const set = new Set(arr)
    return [...set]
  }

  const res = unique([30, 20, 10, 20, 40])
  console.log(res)
</script>

</html>